package cn.com.qingz.core.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;

public class JSONPOSTUtils {

    public static boolean isAjaxRequest(HttpServletRequest request) {
        String requestedWith = request.getHeader("X-Requested-With");
        return requestedWith != null ? "XMLHttpRequest".equals(requestedWith) : false;
    }

    public static boolean isAjaxUploadRequest(HttpServletRequest request) {
        return request.getParameter("ajaxUpload") != null;
    }

    private JSONPOSTUtils() {
    }

    public static void main(String[] args) throws MalformedURLException {



        URL url = new URL("http://zrp.ewei.dev/api/v1/articles/save.json?_app_key=NTI3&_sign=03871effbb448d5dabbc45d4ee886e1f&_timestamp=1473054512713");
        JSONObject object = new JSONObject();
        object.put("title","ffxxffdd" );
        object.put("remark","<p>1111</p>" );
        object.put("recommendIndex",1);
        object.put("closeAnswer",true);
        JSONObject c = new JSONObject();
        c.put("id",1137);
        object.put("topic",c);

        JSONObject d = new JSONObject();
        d.put("id",11561);
        object.put("author",d);





        System.out.println(object.toJSONString());

//        String x ="{\"touser\":\"015015053632369021\",\"agentid\":\"38172764\",\"msgtype\":\"text\",\"text\":{\"content\":\"我是胡九龙\"}}";
        String x = object.toJSONString();


        HttpURLConnection connection ;
        StringBuffer sbuffer=null;
        try {
            //添加 请求内容
            connection= (HttpURLConnection) url.openConnection();
            //设置http连接属性
            connection.setDoOutput(true);// http正文内，因此需要设为true, 默认情况下是false;
            connection.setDoInput(true);// 设置是否从httpUrlConnection读入，默认情况下是true;
            connection.setRequestMethod("POST"); // 可以根据需要 提交 GET、POST、DELETE、PUT等http提供的功能
            //connection.setUseCaches(false);//设置缓存，注意设置请求方法为post不能用缓存
            // connection.setInstanceFollowRedirects(true);

            connection.setRequestProperty("Host", "*******");  //设置请 求的服务器网址，域名，例如***.**.***.***
            connection.setRequestProperty("Content-Type", " application/json");//设定 请求格式 json，也可以设定xml格式的
            connection.setRequestProperty("Accept-Charset", "utf-8");  //设置编码语言
            connection.setRequestProperty("X-Auth-Token", "token");  //设置请求的token
            connection.setRequestProperty("Connection", "keep-alive");  //设置连接的状态
            connection.setRequestProperty("Transfer-Encoding", "chunked");//设置传输编码
            connection.setReadTimeout(10000);//设置读取超时时间
            connection.setConnectTimeout(10000);//设置连接超时时间
            connection.connect();
            OutputStream out = connection.getOutputStream();//向对象输出流写出数据，这些数据将存到内存缓冲区中
            out.write(x.getBytes());            //out.write(new String("测试数据").getBytes());            //刷新对象输出流，将任何字节都写入潜在的流中
            out.flush();
            // 关闭流对象,此时，不能再向对象输出流写入任何数据，先前写入的数据存在于内存缓冲区中
            out.close();
            //读取响应
            if (connection.getResponseCode()==200)            {
                // 从服务器获得一个输入流
                InputStreamReader inputStream =new InputStreamReader(connection.getInputStream());//调用HttpURLConnection连接对象的getInputStream()函数, 将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
                BufferedReader reader = new BufferedReader(inputStream);
                String lines;
                sbuffer= new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    lines = new String(lines.getBytes(), "utf-8");
                    sbuffer.append(lines);                }
                reader.close();
                System.out.println(sbuffer);
            }else{
                System.out.println("请求失败"+connection.getResponseCode());
            }
            //断开连接
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
